package com.dhh.sky.util;

import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.util.Log;
import com.dhh.sky.dao.OperationDAO;
import com.dhh.sky.model.Operation;

public class OperationManager {
	private OperationDAO dao;
	private FileManager fm;
	
	public OperationManager(Context context){
		dao = new OperationDAO(context); 
		fm = new FileManager(context);
	}
	
	public List<Operation> getOperationReady(int action){
		return dao.getOperation(action,Operation.STATUS_READY);
	}
	
	public List<Operation> getOperationFailed(int action){
		return dao.getOperation(action,Operation.STATUS_ERROR);
	}
	
	public List<Operation> getOperationSuccessed(int action){
		return dao.getOperation(action,Operation.STATUS_SUCCESS);
	}
	
	public Operation getOperation(String path,int action){
		return dao.getOperation(path,action);
	}
	
	public boolean removeOperation(String path, int action){
		return dao.removeOperation(path, action);
	}
	
	public void update(Operation op){
		dao.update(op);
	}
	
	public void addDeleteOperation(String[] paths,String parent){
		for(String path : paths){
			dao.addOperation(path, parent, Operation.OP_DELETE, Operation.STATUS_READY,"");
		}
	}
	
	public void addCopyOperation(String[] paths,String target){
		for(String path : paths){
			dao.addOperation(path, target, Operation.OP_COPY, Operation.STATUS_READY,"");
		}
	}
	
	public void addUploadOperation(String[] paths,String target){
		for(String path : paths){
			dao.addOperation(path, target, Operation.OP_UPLOAD, Operation.STATUS_READY,getFileName(path));
		}
	}
	
	public void addCamearUploadOperation(String[] paths,String target){
		for(String path : paths){
			dao.addOperation(path, target, Operation.OP_CAMERA, Operation.STATUS_READY,getFileName(path));
		}
	}
	
	public void addDownloadOperation(String[] paths,String target){
		for(String path : paths){
			dao.addOperation(path, target, Operation.OP_DOWNLOAD, Operation.STATUS_READY,CUtil.buildFileName(fm.getFile(path)));
		}
	}
	
	public void addRenameOperation(String path,String parent,String meta){
		dao.addOperation(path, parent, Operation.OP_RENAME, Operation.STATUS_READY,meta);
	}
	
	public void addNewFolderOperation(String path,String meta){
		dao.addOperation(path, "", Operation.OP_CREATE_FOLDER, Operation.STATUS_READY,meta);
	}
	
	public void addMoveOperation(String[] paths,String target){
		for(String path : paths){
			dao.addOperation(path, target, Operation.OP_MOVE, Operation.STATUS_READY,"");
		}
	}
	
	public void addOpenOperation(String id,String parent,String json){
		dao.addOperation(id, parent, Operation.OP_OPEN, Operation.STATUS_READY,json);
	}
	
	public void removeOperation(int action){
		dao.removeOperation(action);
	}
	
	public List<Operation> getErrorOperation(String path){
		List<Operation> ops = new ArrayList<Operation>();
		List<Operation> elst = dao.getOperation(Operation.STATUS_ERROR);
		try{
			if(elst!=null && elst.size()>0){
				ops.addAll(elst);
				for(Operation op : elst){
					dao.removeOperation(op.getPath(), Integer.valueOf(op.getAction()));
				}
			}
		}catch(Exception e){
			Log.e("SDA", "Get Error Operation", e);
		}
		
		
		return ops;
	}
	
	private String getFileName(String absolutName){
		String name = absolutName;
		int dotIndex=absolutName.lastIndexOf('/');
    	if(dotIndex>=0) {
    		name=absolutName.substring(dotIndex+1,absolutName.length());
    	}
    	
    	return name;
	}
}
